﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ElectronicsMarket.EMServices;
using ElectronicsMarket.InternalService;

namespace ElectronicsMarket.Models
{
    public class Cart
    {
        private List<CartItem> itemCollection = new List<CartItem>();

        public List<CartItem> ItemCollection
        {
            get { return itemCollection; }
            set { itemCollection = value; }
        }

        public void AddItem(PRODUCT product, int quantity)
        {
            CartItem cartItem = ItemCollection.Where(p => p.Product.PRODUCT_ID == product.PRODUCT_ID).FirstOrDefault();
            if (cartItem == null)
            {
                CartItem cartI = new CartItem();
                cartI.Product = product;
                cartI.Quantity = quantity;
                cartI.AddPrice = GetTotalValue();
                ItemCollection.Add(cartI);
            }
            else
                cartItem.Quantity += quantity;
        }

        public void RemoveItems(PRODUCT product)
        {
            ItemCollection.RemoveAll(l => l.Product.PRODUCT_ID == product.PRODUCT_ID);
        }

        public decimal GetTotalValue()
        {
            return ItemCollection.Sum(e => e.Product.PRODUCT_PRICE * e.Quantity);
        }

        public void Clear()
        {
            ItemCollection.Clear();
        }
    }
}